/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Downloader;

import DataBase.Ficheiro;
import java.net.MalformedURLException;

public class SetData {

    private Ficheiro file = new Ficheiro();
    private Downloader cd = new Downloader();

    public void Downloader(String url, String pathLocal, int level) {
        try {
            DownloadFile(url, pathLocal, level);
        } catch (MalformedURLException ex) {
        }

    }

    public void Downloader(Ficheiro obj, int level) {
        try {
            DownloadFile(obj.getCaminhosite(), obj.getCaminholocal(), level);
        } catch (MalformedURLException ex) {
        }

    }

    private void DownloadFile(String url, String pathLocal, int level) throws MalformedURLException {
        int temp = -1;
        String domain;
        try {
            temp = cd.fileDownload(url, pathLocal, level);
            //no caso de tudo ok com o url temp=0
        } catch (MalformedURLException ex) {
        }

        if (temp == 0) {
            this.file.setCaminholocal(cd.getFullLocalPathFile());
            if (cd.getFullWebPath().contains(cd.getFileName())) {
                this.file.setCaminhosite(cd.getFullWebPath());
            } else {
                this.file.setCaminhosite(cd.getFullWebPath() + cd.getFileName());
            }
            domain = socof_webcrawler.SOCOF_WebCrawler.getDOMAIN();
            if (!domain.toLowerCase().contains("http://")) {
                domain = "http://" + domain + "/";
            }
            this.file.setDominio(socof_webcrawler.SOCOF_WebCrawler.getDOMAIN());
            this.file.setFicheiro(cd.getFileName());
            this.file.setNiveis(cd.getNivel());
            this.file.setTamanhosite(cd.getSize());
        } else {
            System.err.println("File does not exists on Server");
            this.file.setEstado(this.file.DB_STATE_INCOMPLETE);
        }
    }

    public boolean DownloadFile() {
        int result = cd.downloadFile();
        boolean devolve;

        switch (result) {
            case 1:
                devolve = true;
                break;
            default:
                devolve = false;
                break;
        }
        return devolve;
    }

    public Ficheiro getFicheiro() {
        return this.file;
    }
}
